<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link href="css/zTreeStyle/zTreeStyle.css" rel="stylesheet" type="text/css">					<!-- zTree CSS函式庫 -->
<link href="css/redmond/jquery-ui-1.10.3.custom.min.css" rel='stylesheet' type='text/css'>
<link href="css/selfTemplate.css" rel="stylesheet" type="text/css">							<!-- 自建CSS函式庫 -->

<script src="js/jquery-1.10.2.min.js"></script>
<script src="js/jquery-ui-1.10.3.custom.js"></script>
<script src="js/jquery.ztree.core-3.5.min.js" type="text/javascript"></script>						<!-- zTree 的核心函式庫，選擇SVN用 -->
<script src="js/jquery.ztree.excheck-3.5.min.js" type="text/javascript"></script>				<!-- zTree 的延伸函式庫，選擇SVN用 -->
<script src="js/jquery.cookie.js" type="text/javascript"></script>								<!-- 操做cookie用 -->
<script src="js/widgets.js"></script>									<!-- 自建函式庫 -->

<style type="text/css">

	div#divTabs {
		vertical-align: text-top;
	}

	div#mainDiv {
		padding-top:20px;
		padding-buttom:20px; 
		padding-right:20px;
		padding-left:15px; 
		
	}
</style>

<script type="text/javascript">


/** 為了產生zTree的參數 **/
var setting = { check:{ enable:true},data:{simpleData: {enable:true}} };
var initialValue = 60;

//刪除分頁
function removeTabs(obj, tName,modId) {
	removeRow(obj,tName);
	
	$("#divTabs > ul > li ").each( function(i){
		$(this).children("a[href='#tab"+modId+"']").remove();
	});
	
	$("#tab"+modId).remove();
	
	//如果分頁已經被刪光，要將標頭一併刪除
	var modCount = $(".modTab").size();
	if( modCount == 0 ) {
		$("#labelDivTabs").remove();
		$("#divTabs").remove();
	}	
}

//主管按下同意或拒絕的鈕
function reply(obj) {
	clearErrorMsg();
	windowFreeze("執行中..");
	
	$(".reply").prop("disabled",true);	//將操作的按鈕給disable
	
	
	$.ajax({ type:"post",url:"ApproveChkIn",dataType:"text",async:false,
		data: {
			toDoListId:$(obj).attr("toDoListId"),
			modUnitId:$("#modUnitId").val(),
			subsysId:$("select#subsysId option:selected").val(),
			creatorId:$(obj).attr("creatorId"),
			empId:$.cookie("empId"),
			chkInId:$(obj).attr("chkInId"),
			isApprove:$(obj).attr("isApprove")
		}, success : function(reses) {
			$("#backToDoList").show();
		}, error : function(xhr, ajaxOptions, thrownError) { errorAjx(xhr); }
	});
	windowUnfreeze();
}

function msgClassfy(arr,fileVer,existReqId) {
	var isIn = false;	//檢查陣列中是否已有此版本的row
	for(var k=0; k< arr.length;k++) {
		if( arr[k].ver == fileVer ) {
			isIn = true;
		}
	}
	if( isIn ) {
		for(var k=0; k< arr.length;k++) {
			if( arr[k].ver == fileVer ) {
				arr[k].msg += REQTOKEN + existReqId;
			}
		}
	} else {
		arr.push( {"ver":fileVer,"msg":existReqId} )
	}
}


//取得指定檔案的歷屆變更
function getFileHistoryRevision(isMR,url,usr,pwd,relativePath,fileName){
	var verSets;
	
	$.ajax({type:"post",url:"ChkFileRevision",dataType:"json",async:false,
		data:{isMR:isMR,url:url,usr:usr,pwd:pwd,relativePath:relativePath,fileName:fileName},
		success : function(reses) {
			verSets = reses;
		},error : function(xhr, ajaxOptions, thrownError) { errorAjx(xhr); }
	});
	
	return verSets;
}

/** 點選欲提交的檔案 **/
function selSVNfile(svnType,modId) {
	clearErrorMsg();
	windowFreeze();
	
	var subsysId = $("select#subsysId option:selected").val();
	
	//先確定該子系統 的SVN 連線無誤，再進行抓取
	// 0 無進行測試 1 沒有為此子系統設定 2 主機不能連 3 帳密不通  4 連線正常
	chkSVN4subsys(subsysId, svnType );
	
	var res= $("#resSVN4subsys").val();
	
	if (res==0) {
		errorMsgShow("SVN連線發生問題，請洽系統人員");
	} else if (res==1) {
		errorMsgShow("沒有為此子系統設定SVN，請洽系統人員");
	} else if (res==2) {
		errorMsgShow("所設定的主機連線失敗，請洽系統人員");
	} else if (res==3) {
		errorMsgShow("所設定的帳密登入失敗，請洽系統人員");
	} else {	//連線成功
		var isMR = (svnType=="MR")?true:false;
	
		$.ajax({ type:'post',url:'QuerySVNfileInJsonBySubsys',dataType:'text',async:false,
			data: {subsysId:subsysId,isMR:isMR,modId:modId }, //送出子系統，以確定要使用該子系統的SVN
			success : function(reses) {
				if ( reses.length != 3 ) {	//因為servlet那裡這樣寫，所以回傳 "]" 表示沒有找到
					reses = reses.substring(1,(reses.length-1));
					
					$.fn.zTree.init( $("#treeDirectory"),setting, JSON.parse(reses) );
					var treeObj = $.fn.zTree.getZTreeObj("treeDirectory");
					treeObj.expandAll(false);
					$("#dialogSVNdirectory").prop("title","選擇SVN路徑").css("visibility", "visible").dialog({
						resizable:true,width:300,modUnital:true,show:"fold",hide:"fold",
						buttons : {
							"確定" : function() {
								
						        var nodes = treeObj.getCheckedNodes(true);
						        
						        //存取既有需求，供等會兒比對 
						        var subsysId=$("select#subsysId option:selected").val();
								var arrReqALL = [];
								$.ajax({type:"post",url:"QueryReqInCra",dataType:"json",async:false,
									data:{subsysId:subsysId,modId:modId,empId:$.cookie("empId")},
									success : function(reses) {
										$.each(reses, function(i, res){
											arrReqALL.push({reqId:res.s2,reqDesc:res.s1});
										});
									}, error : function(xhr, ajaxOptions, thrownError) { errorAjx(xhr); }
								});
								
								var resultStr = "";	//解析繳交註解時，最後給使用者看的字串

						        $(nodes).each(function(i){
						        	if ( (this.icon).indexOf("file") !== -1 ) {	//只要檔案的node, 不要資料夾的node
						        		var fileId = this.id;
										var fileName = this.name;
						        		
										
						        		if ( $("#tbodyFile"+modId+" td[title='"+fileId+"']").size() == 0){	//不加重覆的檔案
						        			var idx= $("#tbodyFile"+modId+" > tr").size()+1;
						        			var inTmp = "<table id='tableReq"+modId+replaceDot(fileId)+"'><thead class='tableFirst'><tr><td><button onclick=\"addReq('Req"+modId+replaceDot(fileId)+"','"+modId+"')\" class='editable' type='button'>+</button></td><th>#</th><th>需求描述</th></tr></thead><tbody id='tbodyReq"+modId+replaceDot(fileId)+"'></tbody></table>";
											var tmp = "<tr class='rowFile"+modId+idx+"'>"+
											"<td class='center'><button id='"+idx+"' onclick=\"removeRow(this,'File"+modId+"')\" value='"+fileId+"' type='button'>-</button></td>"+
											"<td class='idx'>"+idx+"</td>"+
											"<td class='center "+(isMR?"boldRed":"boldGreen")+"'>"+svnType+"</td>"+
											"<td><select id='"+replaceDot(fileId)+"'></select></td>"+
											"<td class='engN tipItems' title='"+fileId+"'>"+fileName+"</td>"+
											"<td><button type='button'>※</button></td>"+
											"<td>"+inTmp+"</td>"+
											"</tr>";
											$("#tbodyFile"+modId).append(tmp);
											
											tmp = $("#divTabs").tabs("option", "active");
											var url = $("#svn"+svnType+"url").val();
											var usr = $("#svn"+svnType+"usr").val();
											var pwd = $("#svn"+svnType+"pwd").val();
											var relativePath = "";
											$.ajax({type:"post",url:"QuerySourceSVN",dataType:"json",async:false,
												data: {subsysId:subsysId,modId:modId },
												success : function(res) {
													relativePath = (isMR?res.s1:res.s2)+"/";
												},error : function(xhr, ajaxOptions, thrownError) { errorAjx(xhr); }
											});
											
											
											var folderPath = (fileId).substring(0,(fileId).lastIndexOf(fileName));
											if ( folderPath != "" ) {
												relativePath += folderPath;	
											}
											
											//結構均為 {ver:版本,msg:字串 } (為了要記錄從那個版本Parse來的，分隔符號亦為REQTONKEN)
											var arrKnown = [];		//既有的需求
											var arrUnknown = [];	//非既有的需求
											var arrWrongFormat = [];//非正整數
											
											
											
											//取得歷代版次
											var verSets = getFileHistoryRevision(isMR,url,usr,pwd,relativePath,fileName);
											$.each(verSets, function(j,res) {
												tmp = "<option value='"+res.l1+"'>"+res.l1+"</option>";
												$("#tbodyFile"+modId+" select#"+replaceDot(fileId) ).append(tmp);
												
												if ( isProcess(res.s1) ) {
													var arrReq = [];		//字串拆解的原始字串，內容物為單純的版本字串
													arrReq = (res.s1).split( REQTOKEN );
													for (var i=0; i<arrReq.length; i++) {	//開始跑字串拆解出來的陣列
														if ( isPositiveNumaric( arrReq[i] ) ) {
															var isNotExist = true;
															for( a=0; a<arrReqALL.length; a++ ) {
																if( arrReq[i]== arrReqALL[a].reqId ) {	//目前拆解的值在既有需求中
																	isNotExist = false;
																	/*開始寫入需求*/
																	var tName = "Req"+modId+fileId;
																	var idx= $("#tbody"+replaceDot(tName)+" > tr").size()+1;
																    tmp = "<tr class='row"+tName+idx+"'>"+
																	"<td class='center'><button type='button' onclick=\"removeRow(this,'"+replaceDot(tName)+"')\">-</button></td>"+
																	"<td class='idx'>"+idx+"</td>"+
																	"<td reqId='"+arrReqALL[a].reqId+"'>"+ arrReqALL[a].reqDesc+"</td>"+
																	"</tr>";
																	$("#tbody"+replaceDot(tName) ).append(tmp);
																	
																	refreshUI(1,"tbody"+replaceDot(tName),false);
																}
															}
															if ( isNotExist ) {
																msgClassfy(arrUnknown,res.l1, arrReq[i]);
															} else {
																msgClassfy(arrKnown,res.l1, arrReq[i]);
															}
														} else {	//內容非正整數
															msgClassfy(arrWrongFormat,res.l1, arrReq[i]);
														}
													}
												}
											});
											
											if( arrWrongFormat.length > 0 ) {
												resultStr += "\n\n檔案："+fileName+"\n\t存在無法判斷的註解  ";
												resultStr += arrWrongFormat[0].msg;
											}
											if( arrUnknown.length > 0 ) {
												if( resultStr.contains("檔案："+fileName) ) {
													resultStr += "\n\t存在非原先申請的需求  ";
												} else {
													resultStr += "\n\n檔案："+fileName+"\n\t存在非原先申請的需求  ";	
												}
												resultStr += arrUnknown[0].msg;
											}
						        		} else {
						        			resultStr += "\n\n檔案："+fileId+"已存在清單中！";
						        		}
						        	}
								});
						        
						        if ( isProcess(resultStr) ) {
						        	alert(resultStr);
						        }
						        $(".tipItems").tooltip();
						        refreshUI(1,"tbodyFile"+modId,false);
								$(this).dialog("close");
							},"關閉" :function() {
								$(this).dialog("close");
							}
						}
					});
				} else {
					alert(modId+"在"+svnType+"的資料夾為空!");
				}
			}, error : function(xhr, ajaxOptions, thrownError) { errorAjx(xhr); }
		});
	} // end of else
	$("#resSVN4subsys").val("0");	//執行完驗證，將它重設為未測試的狀態，即0
	windowUnfreeze();
}


/**
 * 查詢目前可做為繳交依據的變更申請
 * @param subsysId	指定的子系統
 * @param empId	員工ID
 */
function queryAvailableCRA(subsysId,empId){
	$.ajax({type:"post",url:"QueryAvailableCRA",dataType:"json",async:false,
		data:{empId:empId,subsysId:subsysId},
		success : function(reses) {
			if ( reses.length > 0 ) {
				/*	1. 產生選擇清單
				 * 	2. 產生分頁
				*/
				$("#tbodyModsPicker tr").remove();
				
				var tmp="";
				var idx=0;
				
				$.each(reses, function(i, res){
					idx=($("#tbodyModsPicker td.idx").size()+1);
					tmp = "<tr>"+
					  "<td class='center'><input modId='"+res.modId+"' craId='"+res.craId+"' class='tableModsPickerChk' onclick=\"chk('tableModsPickerChk');\" type='checkbox'/></td>"+	//選擇的checkbox
					  "<td class='idx chkRow'>"+idx+"</td>"+		//流水號
					  "<td class='engN chkRow'>"+res.modId+"</td>"+	//模組名稱
					  "</tr>";
					$("#tbodyModsPicker").append(tmp);
					
					//如果原本有選的話，這裡要為它打勾
					if ( $("#tbodyMods > tr > td[modId='"+res+"']").html()!=undefined ) {
						$("#tbodyModsPicker tr:last input[type='checkbox']").prop("checked", true);
					}
				});
				
				//綁定輔助點選
				$(".chkRow").bind("click",function(){
					$(this).parent("tr").children("td:first").children("input[type='checkbox']").click();
				});
				
				//檢查一下，如果所有子項都有勾，那chkAll也要勾
				chk("tableModsPickerChk");
				refreshUI(1,"tbodyModsPicker",false);
				
				var tName = "Mods";
				
				$("#modsPicker").prop("title","選擇模組").css("visibility", "visible").dialog({
					resizable:true,modal:true,show:"fold",hide:"fold",width:"420",async:false,
					buttons : {
						"確定" : function() {
							/* 將checkbox有勾選的項目，加入列表 */
							
							if ( $(".tableModsPickerChk").size()>0 && $("#divTabs").size()==0 ) {
								tmp = "<label id='labelDivTabs' class='thStyle' for='divTabs'>--設定提交設定--</label><div id='divTabs'><ul id='ulTabs'></ul></div>";
								$("#divRightTop").append(tmp);	//產生Tab的主架構
							}

							$(".tableModsPickerChk").each(function(i){
								if ($(this).is(":checked")) {
									var modId = $(this).attr("modId");
									
									if( $("#tbody"+tName+" td[modId='"+modId+"']").size()==0 ){
										//加入 已選擇模組的清單  tbodyMods
										idx= $("#tbody"+tName+" > tr").size()+1;
									    tmp = "<tr class='row"+tName+idx+"'>"+
										"<td class='center'><button type='button' class='hideable' onclick=\"removeTabs(this,'"+tName+"','"+modId+"')\">-</button></td>"+
										"<td class='idx'>"+idx+"</td>"+
										"<td class='engN' modId='"+modId+"'>"+ modId+"</td>"+
										"</tr>";
										$("#tbody"+tName).append(tmp);
										
										//產生下方分頁
										tmp = "<li><a href='#tab"+modId+"'>"+modId+"</li>";
										$("#ulTabs").append(tmp);
										
										tmp = "<table id='tableCommonReq"+modId+"'><thead class='tableFirst'><tr><td><button onclick=\"addReq('CommonReq"+modId+"','"+modId+"')\"  class='editable' type='button'>+</button></td><th>#</th><th>共同對應需求描述</th></tr></thead><tbody id='tbodyCommonReq"+modId+"'></tbody></table>";
										tmp = "<div class='modTab' modId='"+modId+"' id='tab"+modId+"'>"+tmp+
										"<hr><table id='tableFile"+modId+"'><thead class='tableFirst'><tr><td><button class='editable' type='button' id='btnAddFile"+modId+"'>+</button></td><th>#</th><th>來源</th><th>版次</th><th>檔案名稱</th><th>※</th><th>個別對應需求</th></tr></thead><tbody id='tbodyFile"+modId+"'></tbody></table>"+
										"</div>";
										$("#divTabs").append(tmp);
										$("#divTabs button").button();
										
										//點選新增「新增依據」
										$("#btnAddFile"+modId).bind("click", function(){
											clearErrorMsg();
											windowFreeze();
											
											var tmp = "提交那一個SVN repo 的檔案<br><input type='radio' name='repType' value='OL' checked>OL<br><input type='radio' name='repType' value='MR'>MR";
											
											$("#diaConfirm").html(tmp);
											tmp = $("#divTabs").tabs("option", "active");
											var modId = $("#divTabs > ul > li").eq(tmp).text();
											
											$("#diaConfirm").prop("title","選擇欲提交的檔案").css("visibility", "visible").dialog({
												resizable:true,modal:true,show:"fold",hide:"fold",width:"200",async:false,
												buttons : {
													"確定" : function() {
														selSVNfile( $("input[name='repType']:checked").val(),modId );
														$(this).dialog("close");
													},"關閉" :function() {
														$(this).dialog("close");
													}
												}
											});
											windowUnfreeze();
										});
									}
								}
							});
							setMainDivWidth(initialValue);
								
							refreshUI(1,"tbody"+tName,false);
							$("#divTabs").tabs();
							
							
							$(this).dialog("close");
						},"關閉" :function() {
							$(this).dialog("close");
						}
					}
				});
				
			} else {
				errorMsgShow("子系統"+subsysId+"下，未查詢到任何可使用的變更申請，請先提出變更申請");	
			}
		},error : function(xhr, ajaxOptions, thrownError) { errorAjx(xhr); }
	});	
}



$(document).ready(function(){
	init("chkIn.html");
	var t = querySubsys( $.cookie("empId"),$.cookie("pageName"),"subsysId");
	
	if( t==1 ) {
		windowFreeze("畫面初始中");
		
		//為了重覆使用此頁面，利用此cookie來辨別目前的動作
		if ( $.cookie("actType") !=undefined ) {
		
			//表示正在是審查新增模組
			if ( $.cookie("actType") == "review" ) {
				var cir = "";
				$.ajax({	//取得待辦事項清單
					type : 'post',
					url : 'IsComplete',
					dataType : 'text',
					async:false,
					data: {itemType:2,masterId:$.cookie("chkInId")},
					success : function(res) {
						cir = JSON.parse( res );
					}, error : function(xhr, ajaxOptions, thrownError) { errorAjx(xhr); }
				});
				
				$("#btnSubmit").hide();	//送出繳交申請的鈕藏起來
				
				$.removeCookie("actType");			//供頁面辨別目前這個頁面要如何回應
				$("#subsysId").val(cir.subsysId);
				$("#svnMRurl").val(cir.svnMRurl);
				$("#svnMRusr").val(cir.svnMRusr);
				$("#svnMRver").val(cir.svnMRver);
				$("#svnOLurl").val(cir.svnOLurl);
				$("#svnOLusr").val(cir.svnOLusr);
				$("#svnOLver").val(cir.svnOLver);
				$("#isUrgent").attr("checked", (cir.isUrgent=="True"));
				
	 			var tmp = "<label  id='labelDivTabs' class='thStyle' for='divTabs'>--設定提交設定--</label><div id='divTabs'><ul id='ulTabs'></ul></div>";
				$("#divRightTop").append(tmp);	//產生Tab的主架構

				$.each(cir.cios, function(i, res){
					//加入 已選擇模組的清單  tbodyMods
					var modId= res.modId;
					var idx= $("#tbodyMods > tr").size()+1;
				    tmp = "<tr class='rowMods"+idx+"'>"+
					"<td class='center'><button type='button' class='hideable editable' onclick=\"removeTabs(this,'Mods','"+modId+"')\">-</button></td>"+
					"<td class='idx'>"+idx+"</td>"+
					"<td class='engN' modId='"+modId+"'>"+ modId+"</td>"+
					"</tr>";
					$("#tbodyMods").append(tmp);
					
					//產生下方分頁
					tmp = "<li><a href='#tab"+modId+"'>"+modId+"</li>";
					$("#ulTabs").append(tmp);
					
					tmp = "<table id='tableCommonReq"+modId+"'><thead class='tableFirst'><tr><td><button onclick=\"addReq('CommonReq"+modId+"','"+modId+"')\"  class='editable' type='button'>+</button></td><th>#</th><th>共同對應需求描述</th></tr></thead><tbody id='tbodyCommonReq"+modId+"'></tbody></table>";
					tmp = "<div class='modTab' id='tab"+modId+"'>"+tmp+
					"<hr><table id='tableFile"+modId+"'><thead class='tableFirst'><tr><td><button class='editable' type='button' id='btnAddFile"+modId+"'>+</button></td><th>#</th><th>來源</th><th>版次</th><th>檔案名稱</th><th>※</th><th>個別對應需求</th></tr></thead><tbody id='tbodyFile"+modId+"'></tbody></table>"+
					"</div>";
					$("#divTabs").append(tmp);
					$("#divTabs button").button().attr("disabled",true);
					
					//開始加入共同需求
					$.each(res.commonReqs, function(j, com){
						var idx= $("#tbodyCommonReq"+modId+" > tr").size()+1;
						var reqId = com.i1;
						var reqDesc = "";
						$.each(cir.mapReq, function(a,req) {
							if( req.i1==reqId ) {
								reqDesc = req.s1;
							}
						});
								
						tmp = "<tr><td class='center'><button>-</button></td><td class='idx'>"+idx+"</td><td>"+reqDesc+"</td></tr>";
						$("#tbodyCommonReq"+modId).append(tmp)
					});
					refreshUI(1,"tbodyCommonReq"+modId,false);
					
					//開始加入檔案
					$.each(res.chkInfiles, function(j, resFile){
						var svnType = "OL";
						var isMR = resFile.isMR;
						var fileId = resFile.relativePath+resFile.fileName;
						var fileName = resFile.fileName;
						var idx= $("#tbodyFile"+modId+" > tr").size()+1;
						
						if (resFile.isMR) {
							svnType = "MR";
						}
						
	        			var inTmp = "<table id='tableReq"+modId+replaceDot(fileId)+"'><thead class='tableFirst'><tr><td><button onclick=\"addReq('Req"+modId+replaceDot(fileId)+"','"+modId+"')\" class='editable' type='button'>+</button></td><th>#</th><th>需求描述</th></tr></thead><tbody id='tbodyReq"+modId+replaceDot(fileId)+"'></tbody></table>";
						tmp = "<tr class='rowFile"+modId+idx+"'>"+
						"<td class='center'><button class='editable' id='"+idx+"' onclick=\"removeRow(this,'File"+modId+"')\" value='"+fileId+"' type='button'>-</button></td>"+
						"<td class='idx'>"+idx+"</td>"+
						"<td class='center "+(isMR?"boldRed":"boldGreen")+"'>"+svnType+"</td>"+
						"<td><select class='editable' id='"+replaceDot(fileId)+"'></select></td>"+
						"<td class='engN tipItems' title='"+fileId+"'>"+fileName+"</td>"+
						"<td><button type='button'>※</button></td>"+
						"<td>"+inTmp+"</td>"+
						"</tr>";
						$("#tbodyFile"+modId).append(tmp);
						refreshUI(1,"tbodyFile"+modId,false);
						$("select#"+replaceDot(fileId)).append("<option>"+resFile.fileVer+"</option>");
						
						var tName = "Req"+modId+fileId;
						$.each(resFile.indviReqs, function(m, indReq){
							var idx= $("#tbody"+replaceDot(tName)+" > tr").size()+1;
						    tmp = "<tr class='row"+tName+idx+"'>"+
							"<td class='center'><button class='editable' type='button' onclick=\"removeRow(this,'"+replaceDot(tName)+"')\">-</button></td>"+
							"<td class='idx'>"+idx+"</td>"+
							"<td reqId='"+indReq.i1+"'>"+ indReq.s1+"</td>"+
							"</tr>";
							$("#tbody"+replaceDot(tName) ).append(tmp);						
						});
						refreshUI(1,"tbody"+replaceDot(tName),false);
					});
				})
				
				$("#divTabs").tabs();
				
				$(".editable").prop("disabled",true);	//因為是Review，所以將所有輸鈕都disable
	 			
	 			$("#actName").html("--審查繳交申請--");
	 			
	 			tmp="<button class='reply' type='button' onclick='reply(this)' creatorId='"+cir.creatorId+"' toDoListId='"+$.cookie("toDoListId")+"' isApprove='1' chkInId='"+$.cookie("chkInId")+"'>同意</button> &nbsp; "+
	 				"<button class='reply' type='button' onclick='reply(this)' creatorId='"+cir.creatorId+"' toDoListId='"+$.cookie("toDoListId")+"' isApprove='0' chkInId='"+$.cookie("chkInId")+"'>拒絕</button> &nbsp; "+
	 				"<p><textarea class='reply'></textarea><p>"+
	 				"<button id='backToDoList' type='button' onclick=\"window.location='toDoList.html'\">回到待辦事項</button>";
	 			$("#divReview").append(tmp);
	 			$("#backToDoList").hide();
	 			$.removeCookie("chkInId");
	 			$.removeCookie("toDoListId");
			}
		} else {
			$("input[type='text']").val("").prop("maxlength",20).prop("size", $("input[type='text']").prop("maxlength")*0.8 );
			$(".editable").prop("disabled",false);
		}
		
		
		
		$.ajax({type:"post",url:"QuerySubsys4Maintain",dataType:"json",async:false,
			success : function(reses) {
				if ( reses.length > 0 ) {
					$.each(reses, function(i,res) {
						if ( res.subsysId== $("select#subsysId option:selected").val() ){
							$("#svnMRurl").val( res['ap']['s4MR'].s2).prop("size",$("#svnMRurl").val().length*0.9);
							$("#svnMRusr").val( res['ap']['s4MR'].s3).prop("size",$("#svnMRusr").val().length*0.8);
							$("#svnMRpwd").val( res['ap']['s4MR'].s4);
							$.ajax({type:"post",url:"QuerySVNver",dataType:"text",async:false,
								data: {url:$("#svnMRurl").val(),usr:$("#svnMRusr").val(),pwd:$("#svnMRpwd").val() }, //送出子系統，以確定要使用該子系統的SVN
								success : function(reses) {
									if ( isPositiveNumaric( reses ) ) {
										$("#svnMRver").val( reses).prop("size",$("#svnMRver").val().length);	
									} else {
										$(".editable").prop("disabled",true);
										errorMsgShow("SVN連線發生錯誤，請與系統管理人員連絡",reses);
									}
								},error : function(xhr, ajaxOptions, thrownError) { errorAjx(xhr); }
							});
							
							$("#svnOLurl").val( res['ap']['s4OL'].s2).prop("size",$("#svnOLurl").val().length*0.9);
							$("#svnOLusr").val( res['ap']['s4OL'].s3).prop("size",$("#svnOLusr").val().length*0.8);
							$("#svnOLpwd").val( res['ap']['s4OL'].s4);
							$.ajax({type:"post",url:"QuerySVNver",dataType:"text",async:false,
								data: {url:$("#svnOLurl").val(),usr:$("#svnOLusr").val(),pwd:$("#svnOLpwd").val() }, //送出子系統，以確定要使用該子系統的SVN
								success : function(reses) {
									if ( isPositiveNumaric( reses ) ) {
										$("#svnOLver").val( reses).prop("size",$("#svnOLver").val().length);	
									} else {
										$(".editable").prop("disabled",true);
										errorMsgShow("SVN連線發生錯誤，請與系統管理人員連絡",reses);
									}
								},error : function(xhr, ajaxOptions, thrownError) { errorAjx(xhr); }
							});
						}
					});
				} else {
					$("button").attr("disabled",true);
				}
			},error : function(xhr, ajaxOptions, thrownError) { errorAjx(xhr); }
		});
		
		
		$("#btnAddMod").bind("click", function(){
			clearErrorMsg();
			windowFreeze();
			queryAvailableCRA( $("select#subsysId option:selected").val(),$.cookie("empId"));
			windowUnfreeze();
		});
		
		//整批繳交的資安檢查報告
		$("#btnAuditALL").bind("click", function(){
			clearErrorMsg();
			windowFreeze();
			queryAvailableCRA( $("select#subsysId option:selected").val(),$.cookie("empId"));
			windowUnfreeze();
		});
		
		
		//按下送出繳交申請
		$("#btnSubmit").bind("click", function(){
			clearErrorMsg();
			windowFreeze();

			/**開始產生json格式後傳*****/
			/**會有多個物件，如可模組、檔案清單*****/
			var modTabs =[];
			var isEveryAllReady = true;	//先假設所有需要的資料皆有準備好
			
			$("div.modTab").each( function(i){
				
				var modId = $(this).attr("modId");
				var commonReqs = [];
				var chkInFiles = [];
				
				//擷取共同需求
				$("#tbodyCommonReq"+modId+" > tr").each( function(j) {
					var reqId = $(this).children("td:last").attr("reqId");	//需求ID
					commonReqs.push({"i1":reqId,"s1":""});					//繳交時不需附上需求描述
				});
				
				
				//擷取繳交的檔案
				$("#tbodyFile"+modId+" > tr").each( function(j) {
					var isMR = $(this).children("td").eq(2).html()=="MR";	
					var fileName = $(this).children("td").eq(4).html();
					var fileId = $(this).children("td").eq(4).attr("title");
					var relativePath = $(this).children("td").eq(4).attr("title");
					var tt = relativePath.lastIndexOf(fileName);
					if ( tt== 0) {
						relativePath ="";
					} else {
						relativePath = relativePath.substring(0, tt) ;	
					}
					
					var fileVer = $(this).children("td").eq(3).children("select").children("option:selected").val();
					var indviReqs = [];
					
					//擷取個別需求
					$("#tbodyReq"+modId+replaceDot(fileId)+" tr").each( function(j) {
						var reqId = $(this).children("td:last").attr("reqId");	//需求ID
						indviReqs.push({"i1":reqId,"s1":""});					//繳交時不需附上需求描述
					});
					
					var tmp= {"chkInDetailId":(j+1),"isMR":isMR,"fileName":fileName,"relativePath":relativePath,"fileVer":fileVer,"indviReqs":indviReqs};
					chkInFiles.push( tmp );
				});
				
				if( chkInFiles.length==0 || commonReqs.length == 0 ) {
					isEveryAllReady = false;
				}
				
				
				modTabs.push( {"modId":modId,"commonReqs":commonReqs,"chkInfiles":chkInFiles } );
			});
			
			if( isEveryAllReady ) {
				var chkInRecord={
					"subsysId":$("select#subsysId option:selected").val(),	//子系統
					"isUrgent":$("#isUrgent").attr("checked"),				//是否為緊急上線
					"isDailyBuild":false,									//是否為每日建置
					"svnMRurl":$("#svnMRurl").val(),						//MR repo 的URL
					"svnMRusr":$("#svnMRusr").val(),						//MR repo 的登入帳號
					"svnMRver":$("#svnMRver").val(),						//MR repo 的版次
					"svnOLurl":$("#svnOLurl").val(),						//OL repo 的URL
					"svnOLusr":$("#svnOLusr").val(),						//OL repo 的登入帳號
					"svnOLver":$("#svnOLver").val(),						//OL repo 的版次
					"cios":modTabs											//繳交的程式清單
				}
				/*****開始產生json格式後傳**/
			
				chkInRecord = JSON.stringify(chkInRecord);
			
				$.ajax({type:"post",url:"CreateChkInRecord",dataType:"text",async:false,
					data:{ chkInRecordJson:chkInRecord,empId:$.cookie("empId") },
					success : function(reses) {
						windowUnfreeze();
						if (reses != "Done") {
							errorMsgShow(reses);
						} else { alert("完成"); }
					},error : function(xhr, ajaxOptions, thrownError) { errorAjx(xhr); }
				});
			} else {
				$("#errShortMsg").html("資料不完整，不得繳交");
			}
			
			windowUnfreeze();
		});
		
		setMainDivWidth(initialValue);
		
		windowUnfreeze();
		$("button").button();
	}
});
	
</script>

</head>
<body>
	<div id="header"></div>		<!-- 畫面抬頭 -->
	<div id="divReview"></div>	<!-- 主管review用 -->
	
	<div id="mainDiv" style="position:relative">
		<button class="editable" type="button" id="btnSubmit">送出繳交申請</button>
		<hr><br>
		
		<div id="divRightTop" style="position:absolute;padding-left:10px;padding-right:10px;right:0;"></div>
		<table id="tableInfo">
			<thead class="tableFirst">
				<tr>
					<th class="right">子系統:</th>
					<td colspan="3" class="left"><select class="editable" id="subsysId"><option>-請選擇-</option></select></td>
				</tr>
				<tr>
					<th class="midVer right" rowspan="2">MR repo:<br>登入帳號:</th>
					<td colspan="3" class="left"><input class="engN" type='text' id="svnMRurl" readonly></td>
				</tr>
				<tr>
					<td class="left"><input class="engN" type='text' id="svnMRusr" readonly value='dpssvn01' ><input type="hidden" id="svnMRpwd"></td>
					<th class="right widthMin">目前版次：</th><td class="left"><input class="engN" type='text' id="svnMRver" readonly></td>
				</tr>
				<tr>
					<th class="midVer right" rowspan="2">OL repo:<br>登入帳號:</th>
					<td colspan="3" class="left"><input class="engN" type='text' id="svnOLurl" readonly></td>
				</tr>
				<tr>
					<td class="left"><input class="engN" type='text' id="svnOLusr" readonly value='dpssvn01' ><input type="hidden" id="svnOLpwd"></td>
					<th class="right widthMin">目前版次：</th><td class="left"><input class="engN" type='text' id=svnOLver readonly></td>
				</tr>
				<tr>
					<th class="right">資安檢查報告</th><td class="left"><button id="btnAuditALL" type="button">※</button></td>
					<th class="right">緊急上線</th><td class="left"><input class="editable" type="checkbox" id="isUrgent"></td>
					
				</tr>
			</thead>
		</table>
		
		<table id="tableMods">
			<thead class="tableFirst">
				<tr><td colspan="3">&nbsp;</td></tr>
				<tr><th class="center" colspan="3">欲提交模組</th></tr>
				<tr>
					<td><button class="hideable editable" type="button" id="btnAddMod">+</button></td>
					<th class="center">#</th>
					<th>模組ID</th>
				</tr>
			</thead>
			<tbody id="tbodyMods"></tbody>
			
			<tfoot><tr><td colspan="3" rowspan="2">&nbsp;</td></tr></tfoot>
		</table>
		<p><p><p>
	</div>
	
</body>


<!-- 單純確認的對話框 -->
<div class="dialog" id="diaConfirm"></div>

<!-- 選擇繳交的模組 -->
<div class="dialog" id="modsPicker">
	<table id="tableModsPicker">
		<thead class="tableFirst">
			<tr>
				<td colspan="3"></td>
			</tr>
			<tr>
				<th class="center"><input id="tableModsPickerChkALL" onclick="chkALL(this);" type="checkbox" /></th>
				<th class="center">#</th>
				<th>模組ID</th>
			</tr>
		</thead>
		<tbody id="tbodyModsPicker"></tbody>
	</table>
</div>

<!-- 選取需求的對話窗 -->
<div class="dialog" id="reqsPicker">
	<table id="tableReqPicker">
		<thead class="tableFirst">
			<tr>
				<th class="center"><input id="tableReqPickerChkALL" onclick="chkALL(this);" type="checkbox" /></th>
				<th>#</th>
				<th>需求描述</th>
				<th>需求ID</th>
				<th>創建日期</th>
				<th>創建人員</th>
			</tr>
		</thead>
		<tbody id="tbodyReqPicker"></tbody>
	</table>
</div>

<!-- 資安檢查報告的對話窗 -->
<div class="dialog" id="auditPicker">
	<table id="tableAuditPicker">
		<thead class="tableFirst">
			<tr>
				<th class="center"><input id="tableAuditPickerChkALL" onclick="chkALL(this);" type="checkbox" /></th>
				<th>#</th>
				<th>需求描述</th>
				<th>需求ID</th>
				<th>創建日期</th>
				<th>創建人員</th>
			</tr>
		</thead>
		<tbody id="tbodyAuditPicker"></tbody>
	</table>
</div>


<div class="dialog" id="dialogSVNdirectory">
	<ul id="treeDirectory" class="ztree"></ul>
</div>

</html>